<?php
	$isDoctorArrayValid = true;
	$isPatientArrayValid = true;
	//if( $argc == 1 ){
	//	die("No Arguments!!!");
	//}else if( $argc == 2){
	//	 $doctorFirstName = $argv[1];
	//}else{
	//	$doctorFirstName = $argv[1];
	//	$doctorSurname = $argv[2];
	//}

	$fromEncoding = "ISO-8859-1";
	$toEncoding = "UTF-8";
	//$toEncoding = "ISO-8859-7";
	
	
	
	$doctorArray = array(120, 151, 946, 1236, 1450);
	$patientArray = array(1130209, 1151106, 1160596, 1180899, 1280111, 2130598, 2191205, 2220300, 2260712, 4091298, 4140802, 4270202, 6280610, 8110399, 9270594, 10150597, 14210512, 17300403);
	
	
	
	//odbc_do($connid, "set names utf8");
	
	$conn_transition = @mysqli_connect("localhost:3306", "root", "root", "clinic_system_transition") or die("cann't connect the mysql db");
	mysqli_set_charset($conn_transition, "utf8");
	
	//$connid = @odbc_connect("Patkey", "", "", SQL_CUR_USE_ODBC) or die("connect error!!");
	date_default_timezone_set("Europe/Dublin");
	
	$conn = @mysqli_connect("localhost:3306", "root", "root", "Clinic_system") or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	
	
	
	//$getDoctorNoSql = "select Doctor_no from doctor where doctor_first_name='$doctorFirstName' ";
	//if( strcmp($doctorSurname, "") != 0 ){
	//	$getDoctorNoSql .= " and doctor_surname='$doctorSurname' ";
	//}
	
	//echo $getDoctorNoSql."\r\n";
	//$doctorNoResult = mysqli_query($conn, $getDoctorNoSql);
	//if( $row = mysqli_fetch_array($doctorNoResult)){
	//	$doctorNo = $row["Doctor_no"];
	//}else{
	//	die("the name has no Correspondence in the mysql!!!");
	//}
	$getPatientSql = "select * from patient ";//where `Doctors First Name` = '$doctorFirstName' ";
	$hasParameter = false;
	
	if( count($doctorArray) != 0 && $isDoctorArrayValid == true ){


		foreach( $doctorArray as $doctorNo ){
			if( $hasParameter == false ){
				$getPatientSql .= " where ";
				$hasParameter = true;
			}else{
				$getPatientSql .= " or ";
				$hasParameter = true;
			}
			$getPatientSql .= " doctor_no = $doctorNo ";
		}
	}
	
	if( count($patientArray) != 0 && $isPatientArrayValid == true ){

		
		foreach( $patientArray as $patientNo ){
			if( $hasParameter == false ){
				$getPatientSql .= " where ";
				$hasParameter = true;
			}else{
				$getPatientSql .= " or ";
				$hasParameter = true;
			}
			$getPatientSql .= " Patient_no = $patientNo ";
		}
	}
	
	
	echo $getPatientSql."\r\n";
	//echo $getPatientSql."\r\n";
	
	$patientResult = mysqli_query($conn_transition, $getPatientSql);
	
	while( $row = mysqli_fetch_array($patientResult) ){
	
		$patientNo = mysqli_real_escape_string($conn, $row["Patient_no"]);
		$firstName = mysqli_real_escape_string($conn, mb_convert_encoding($row["First_name"], $toEncoding, $fromEncoding));
		$surname = mysqli_real_escape_string($conn, mb_convert_encoding($row["Surname"], $toEncoding, $fromEncoding));
		$sex = mysqli_real_escape_string($conn, mb_convert_encoding($row["sex"], $toEncoding, $fromEncoding));
		$dateOfBirth = mysqli_real_escape_string($conn, mb_convert_encoding($row["Date_birth"], $toEncoding, $fromEncoding));
		$Telephone = mysqli_real_escape_string($conn, mb_convert_encoding($row["mobile"], $toEncoding, $fromEncoding));
		$landline = mysqli_real_escape_string($conn, mb_convert_encoding($row["landline"], $toEncoding, $fromEncoding));
		$address = mysqli_real_escape_string($conn, mb_convert_encoding($row["address"], $toEncoding, $fromEncoding));
		$pastHistory = mysqli_real_escape_string($conn, mb_convert_encoding($row["past_history"], $toEncoding, $fromEncoding));
		$insurer = mysqli_real_escape_string($conn, mb_convert_encoding($row["insurer"], $toEncoding, $fromEncoding));
		$InsuranceNo = mysqli_real_escape_string($conn, mb_convert_encoding($row["insurer_no"], $toEncoding, $fromEncoding));
		$holderName = mysqli_real_escape_string($conn, mb_convert_encoding($row["holder_name"], $toEncoding, $fromEncoding));
		$dateLastAk = mysqli_real_escape_string($conn, mb_convert_encoding($row["date_last_ak"], $toEncoding, $fromEncoding));
		$dateLastSl = mysqli_real_escape_string($conn, mb_convert_encoding($row["date_last_sl"], $toEncoding, $fromEncoding));
		$dateLastWait = mysqli_real_escape_string($conn, mb_convert_encoding($row["date_last_wait"], $toEncoding, $fromEncoding));
		$drugsAndAllergies = mysqli_real_escape_string($conn, mb_convert_encoding($row["drugs_allergies"], $toEncoding, $fromEncoding));
		
		$updatePatientSql = "update patient set First_name='".$firstName."', Surname='".$surname."', sex='$sex', ";
		
		if( strcmp(trim($dateOfBirth), "") == 0 && strcmp(strtotime($dateOfBirth), "") == 0 ){
			$updatePatientSql .= " Date_birth=null, ";
		}else{
			$dateOfBirth = date("Y/m/d", strtotime($dateOfBirth));
			$updatePatientSql .= " Date_birth=str_to_date('$dateOfBirth', '%Y/%m/%d '),";
			//echo "date birth:$dateOfBirth\r\n";
		}
		
		$updatePatientSql .= "mobile='$Telephone', landline='$landline', address='$address', past_history='$pastHistory', insurer='$insurer', insurer_no='$InsuranceNo', holder_name='$holderName', drugs_allergies='$drugsAndAllergies', ";
		
		if( strcmp(trim($dateLastAk), "") == 0 && strcmp(strtotime($dateLastAk), "") == 0 ){
			$updatePatientSql .= "date_last_ak=null,";
		}else{
			$dateLastAk = date("Y/m/d", strtotime($dateLastAk));
			$updatePatientSql .= "date_last_ak=str_to_date('$dateLastAk', '%Y/%m/%d'),";
		}
			
		if( strcmp(trim($dateLastSl), "") == 0 && strcmp(strtotime($dateLastSl), "") == 0 ){
			$updatePatientSql .= "date_last_sl=null,";
		}else{
			$dateLastSl = date("Y/m/d", strtotime($dateLastSl));
			$updatePatientSql .= "date_last_sl=str_to_date('$dateLastSl', '%Y/%m/%d'),";
		}
			
		if( strcmp(trim($dateLastWait), "") == 0 && strcmp(strtotime($dateLastWait), "") == 0  ){
			$updatePatientSql .= "date_last_wait=null";
		}else{
			$dateLastWait = date("Y/m/d", strtotime($dateLastWait));
			$updatePatientSql .= "date_last_wait=str_to_date('$dateLastWait', '%Y/%m/%d')";
		}
		
		$updatePatientSql .= " where Patient_no=$patientNo";
		
		$updateResult = mysqli_query($conn, $updatePatientSql);
		echo "update patientNo=$patientNo result=$updateResult\r\n";
				
	}
	mysqli_query($conn, "COMMIT");
	mysqli_close($conn);
?>